using DesktopWidgetApp.Shared;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace DesktopWidgetApp.Client.Data
{
    /// <summary>
    /// 简化版状态仓储实现
    /// 使用内存存储管理状态数据（当前版本不使用SQLite）
    /// </summary>
    public class SqliteStatusRepository
    {
        // 使用内存列表作为临时存储
        private static List<Status> _inMemoryStatuses = new List<Status>();
        private static object _lock = new object();

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="databasePath">数据库路径（当前版本不使用）</param>
        public SqliteStatusRepository(string databasePath)
        {
            // 初始化内存存储
            // 在实际应用中，这里应该初始化数据库连接
        }

        /// <summary>
        /// 获取指定用户的所有状态记录
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns>状态记录列表</returns>
        public Task<IEnumerable<Status>> GetByUserIdAsync(Guid userId)
        {
            lock (_lock)
            {
                var result = _inMemoryStatuses
                    .Where(s => s.UserId == userId)
                    .OrderByDescending(s => s.StartTime)
                    .ToList()
                    .AsEnumerable();
                return Task.FromResult(result);
            }
        }

        /// <summary>
        /// 获取指定用户的当前状态
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns>当前状态</returns>
        public Task<Status?> GetCurrentStatusByUserIdAsync(Guid userId)
        {
            lock (_lock)
            {
                var result = _inMemoryStatuses
                    .Where(s => s.UserId == userId && s.EndTime == null)
                    .OrderByDescending(s => s.StartTime)
                    .FirstOrDefault();
                return Task.FromResult(result);
            }
        }

        /// <summary>
        /// 添加新的状态
        /// </summary>
        /// <param name="status">要添加的状态</param>
        public Task AddStatusAsync(Status status)
        {
            lock (_lock)
            {
                if (status.Id == Guid.Empty)
                {
                    status.Id = Guid.NewGuid();
                }
                if (status.StartTime == DateTime.MinValue)
                {
                    status.StartTime = DateTime.Now;
                }
                _inMemoryStatuses.Add(status);
                return Task.CompletedTask;
            }
        }

        /// <summary>
        /// 结束用户的当前状态
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="endTime">结束时间</param>
        public Task EndCurrentStatusAsync(Guid userId, DateTime endTime)
        {
            lock (_lock)
            {
                var currentStatus = _inMemoryStatuses
                    .Where(s => s.UserId == userId && s.EndTime == null)
                    .OrderByDescending(s => s.StartTime)
                    .FirstOrDefault();
                if (currentStatus != null)
                {
                    currentStatus.EndTime = endTime;
                }
                return Task.CompletedTask;
            }
        }

        /// <summary>
        /// 获取指定类型的所有状态
        /// </summary>
        /// <param name="statusType">状态类型</param>
        /// <returns>指定类型的状态列表</returns>
        public Task<IEnumerable<Status>> GetByStatusTypeAsync(StatusType statusType)
        {
            lock (_lock)
            {
                var result = _inMemoryStatuses
                    .Where(s => s.Type == statusType)
                    .OrderByDescending(s => s.StartTime)
                    .ToList()
                    .AsEnumerable();
                return Task.FromResult(result);
            }
        }

        /// <summary>
        /// 简化的版本不实现其他复杂功能
        /// </summary>
    }
}